myApp.controller('UCInitController', function ($scope, $http, $stateParams, ngDialog) {

    $scope.info = {};
    $scope.info.index = 1;
    $scope.info.vmInfo = {};
    $scope.info.vmInfo.ip = $stateParams.selectVm;
    $scope.info.vcloud = {};
    $scope.info.zones = uCloud_zones;

    $scope.info.initDatabase = true;
    $scope.info.dataFrom = 'demo';

    $scope.info.oracleCount = 0;
    $scope.info.mysqlCount = 0;
    $scope.info.appCount = 0;
    $scope.info.totalTime = 0;

    //获取vm信息
    if($scope.info.vmInfo.ip!=''){
        getUCloud();
        getZones();
    };

    $scope.previousBtn = function () {
        if ($scope.info.index > 1) {
            if ($scope.info.index == 4) {
                if ($scope.info.initDatabase) {
                    $scope.info.index = 3;
                } else {
                    $scope.info.index = 2;
                }
            } else {
                $scope.info.index = $scope.info.index - 1;
            }
        } else {
            $scope.info.index = 1;
        }
    };

    $scope.nextBtn = function () {
        var count = 0;
        angular.forEach($scope.info.zones, function (zone) {
            if (zone.available) {
                count = 1;
                return;
            }
        });

        if (count == 0) {
            openDialog(ngDialog, $scope, "请至少选择一个环境进行初始化!", false, function () {
            });
            return;
        }

        if ($scope.info.index < 4) {
            if ($scope.info.index == 2) {
                if ($scope.info.initDatabase) {
                    $scope.info.index = 3;
                } else {
                    $scope.info.index = 4;
                }
            } else {
                $scope.info.index = $scope.info.index + 1;
            }
        } else {
            $scope.info.index = 4;
        }
        if ($scope.info.index == 4) {
            $scope.totalCount();
        }
    };

    $scope.totalCount = function () {
        $scope.info.oracleCount = 0;
        $scope.info.mysqlCount = 0;
        $scope.info.appCount = 0;
        $scope.info.totalTime = 0;
        angular.forEach($scope.info.zones, function (zone) {
            if (zone.available) {
                $scope.info.oracleCount++;
                $scope.info.mysqlCount = $scope.info.mysqlCount + 10;
                $scope.info.appCount = $scope.info.appCount + 10;
                $scope.info.totalTime++;
            }
        });
    };

    //初始化
    $scope.doInit = function () {
        openDialog(ngDialog, $scope,
            "即将开始初始化,大约需要" + $scope.info.totalTime + "小时时间, 期间请不要重启或登录uCloud作操作,否则造成不可预期的结果."
            , true, function (flag) {
                if (!flag) {
                    return;
                }
                var zones = '';
                angular.forEach($scope.info.zones, function (zone) {
                    if (zone.available) {
                        zones = zones + zone.zone_code + ',';
                    }
                });
                zones = zones.substring(0, zones.length - 1);
                var data_from = 'none';
                if ($scope.info.initDatabase) {
                    data_from = $scope.info.dataFrom;
                }
                var ip = $scope.info.vmInfo.ip;
                if (typeof ip == 'undefined'
                    || ip == 'undefined'
                    || ip == '') {
                    alert("虚拟机ip为空!");
                    return;
                }
                ;
                $.ajax({
                    type: "put",
                    url: getUCloudAddress(ip) + '/zones/install?only_db=false&zones=' + zones + "&data_from=" + data_from,
                    dataType: "JSON",
                    contentType: "application/json",
                    async: false,
                    success: function (data) {
                        window.location.href = "#/menu/ucProgress/" + $scope.info.vmInfo.ip;
                    }
                });
            });
    };

    function getUCloud() {
        var ip = $scope.info.vmInfo.ip;
        if (typeof ip == 'undefined'
            || ip == 'undefined'
            || ip == '') {
            alert("虚拟机ip为空!");
            return;
        }
        ;

        $http.get(getUCloudAddress(ip) + "/vcloud/stat", {
            headers: {'Accept': 'application/json'}
        }).success(function (data) {
            $scope.info.vcloud = data;
            $scope.info.vmStatus = $scope.info.vcloud.status;
            $scope.info.vcloud.memory = bytesToSize($scope.info.vcloud.memory);
            $scope.info.vcloud.disk = bytesToSize($scope.info.vcloud.disk);
            $scope.info.vcloud.disk_free = bytesToSize($scope.info.vcloud.disk_free);

        }).error(function () {
            $scope.info.vmStatus = "stop";
        });
    };

    function getZones() {
        var ip = $scope.info.vmInfo.ip;
        if (typeof ip == 'undefined'
            || ip == 'undefined'
            || ip == '') {
            alert("虚拟机ip为空!");
            return;
        }
        ;

        $http.get(getUCloudAddress(ip) + "/zones/", {
            headers: {'Accept': 'application/json'}
        }).success(function (data) {
            // $scope.info.zones=data;
            angular.forEach($scope.info.zones, function (zone) {
                for (var i = 0; i < data.length; i++) {
                    zone.isSupport = false;
                    if (data[i].zone_code == zone.zone_code) {
                        zone.isSupport = true;
                        zone.available = data[i].available;
                        break;
                    }
                }
            });
        }).error(function () {
            $scope.info.vmStatus = "stop";
        });
    };
});